package com.starmark.gateway.auth.provider.web;


import com.starmark.auth.common.util.UserUtil;
import com.starmark.core.framework.base.BaseTree;
import com.starmark.core.framework.constants.SysCommonConstant;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.core.framework.web.SysCommonBeanDataController;
import com.starmark.gateway.auth.provider.entity.GatewayAuthResource;
import com.starmark.gateway.auth.provider.service.IGatewayAuthResourceService;
import com.starmark.gateway.auth.provider.vo.GatewayAuthResourceTreeVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 通用获取data的相关接口
 *
 * @author starmark
 * <p>
 * 2018年08月13日下午7:21:41
 */
@RestController
@RequestMapping(value = "/gateway/auth/common")
public class GatewayAuthCommonController extends SysCommonBeanDataController {

    @Autowired
    private IGatewayAuthResourceService authResourceService;

    @ApiOperation(value = "查询用户的所看菜单")
    @GetMapping(value = "/getSelectTree")
    public List<GatewayAuthResourceTreeVo> getSelectTree() {
        String projectCode = UserUtil.getCurrentUser().getProjectCode();

        return BaseTree.build(BeanMapper.mapList(authResourceService.getResourceTree(projectCode), GatewayAuthResourceTreeVo.class),
                null);

    }

    @ApiOperation(value = "查询用户的所看菜单")
    @GetMapping(value = "/getUserMenu")
    public List<GatewayAuthResourceTreeVo> getUserMenu() {
        // if (Objects.requireNonNull(UserUtil.getSecurityUser()).isAdmin()) {

        return wrapper(authResourceService.queryList(new HashMap<>()));
        // } else {
        //     Set<String> roleIds = UserUtil.getRoles();
        //    return wrapper(sysMenuService.getAuthMenu(roleIds));
        //  }
    }

    private List<GatewayAuthResourceTreeVo> wrapper(List<GatewayAuthResource> menuList) {

        List<GatewayAuthResourceTreeVo> gatewayAuthResourceTreeVos = BeanMapper.mapList(menuList, GatewayAuthResourceTreeVo.class);
        return BaseTree.build(gatewayAuthResourceTreeVos, SysCommonConstant.ROOT);
    }
}
